package json;


import util.BannerUtil;
import util.DowLoadUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.SocketException;
import java.net.URL;
import java.net.URLDecoder;

/**
 * 图形界面
 */

public class SwingMain extends JFrame {

    //编码关键字
    private static String encode;
    private static int name = 1;
    private static InputStream inputStream = null;
    private static FileOutputStream outputStream = null;

    //删除文件数量
    private static int c = 1;
    //设置系统颜色
    private static String[] setColorList = {"无颜色", "红色", "白色", "绿色", "黑色", "蓝色"};
    //设置系统字体
    private static String[] setFontList = {"宋体", "微软雅黑", "楷体"};
    //设置选择颜色
    private static String[] list = {"全部颜色", "黑色", "粉色", "蓝色", "红色", "黄色", "紫色", "绿色", "青色", "橙色", "棕色"};
    //下载图片尺寸
    private static String[] sizeList = {"全部尺寸", "特大尺寸", "大尺寸", "中尺寸", "小尺寸"};
    //设置下载页数
    private static Integer[] pageList = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    //设置字体宋体
    private Font font1 = new Font("宋体", Font.BOLD, 20);
    //设置字体微软雅黑
    private Font font2 = new Font("微软雅黑", Font.BOLD, 20);
    //设置字体楷体
    private Font font3 = new Font("楷体", Font.BOLD, 20);


    //线程
    private Thread threadDowLoad;
    //关键字
    private String stopText;
    //下载路径
    private String text;
    private String colors;

    //大小
    private String sizeS;
    //页数
    private Integer pages;
    //页数下拉列表组件
    private JComboBox<Integer> page;
    //颜色下拉列表组件
    private JComboBox<String> color;

    //图片大小下拉框组件
    private JComboBox<String> size;
    //组件输入框路径path;
    private JTextField path;
    //组件输入框关键字
    private JTextField antiStop;

    //文本域
    private JTextArea area;
    //设置按钮
    private JButton set;
    //设置面板
    private JDialog dialogSet;
    //页面的主面板容器
    private Container contentPane;

    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JLabel jLabelSize;
    private JLabel jLabel;

    public SwingMain() {
        super("百度爬虫程序");
        setBounds(200, 20, 1114, 850);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        contentPane = getContentPane();
        contentPane.setFont(font1);
        set = new JButton("设置");
        contentPane.add(set);
        set.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                set();
                dialogSet.setVisible(true);
                dialogSet.pack();
                dialogSet.validate();

            }
        });
        area = new JTextArea(35, 100);
        JScrollPane scroll = new JScrollPane(area);
        area.setEditable(false);
        contentPane.add(scroll);
        validate();
        //下载路径
        jLabel2 = new JLabel("请输入下载路径");
        jLabel2.setFont(font1);
        contentPane.add(jLabel2);
        path = new JTextField(20);
        path.setFont(font1);
        contentPane.add(path);
        //关键字
        jLabel = new JLabel("请输入关键字");
        jLabel.setFont(font1);
        contentPane.add(jLabel);
        antiStop = new JTextField(20);
        antiStop.setFont(font1);
        contentPane.add(antiStop);
        //图片大小
        jLabelSize = new JLabel("图片大小");
        jLabelSize.setFont(font1);
        contentPane.add(jLabelSize);
        size = new JComboBox<String>(sizeList);
        size.setFont(new Font("宋体", Font.BOLD, 18));
        contentPane.add(size);

        //请输入下载页数/每页30张
        jLabel3 = new JLabel("请输入下载页数/每页30张");
        jLabel3.setFont(font1);
        contentPane.add(jLabel3);
        page = new JComboBox<Integer>(pageList);
        page.setEditable(true);
        page.setFont(font1);
        contentPane.add(page);

        jLabel1 = new JLabel("选择你的喜欢的颜色");
        jLabel1.setFont(font1);
        contentPane.add(jLabel1);
        color = new JComboBox<String>(list);
        color.setFont(font1);
        contentPane.add(color);
        JButton dowLoad = new JButton("下载");
        Dimension preferredSize = new Dimension(200, 50);//设置尺寸
        dowLoad.setPreferredSize(preferredSize);
        JButton stop = new JButton("关闭(不建议)");
        stop.setPreferredSize(preferredSize);


        stop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                threadDowLoad.stop();
                area.append("                                     线程已经停止！\n");
            }
        });
        contentPane.add(dowLoad);
        contentPane.add(stop);
        validate();

        BannerUtil.printStr(area);

        dowLoad.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //下载
                //获取页数
                pages = (Integer) page.getSelectedItem();
                //获取颜色
                colors = (String) color.getSelectedItem();
                if (colors.equals("全部颜色")) {
                    colors = "0";
                }
                if (colors.equals("黑色")) {
                    colors = "512";
                }
                if (colors.equals("粉色")) {
                    colors = "64";
                }
                if (colors.equals("蓝色")) {
                    colors = "16";
                }
                if (colors.equals("红色")) {
                    colors = "1";
                }
                if (colors.equals("黄色")) {
                    colors = "2";
                }
                if (colors.equals("紫色")) {
                    colors = "32";
                }

                if (colors.equals("绿色")) {
                    colors = "4";
                }
                if (colors.equals("青色")) {
                    colors = "8";
                }
                if (colors.equals("橙色")) {
                    colors = "256";
                }
                if (colors.equals("棕色")) {
                    colors = "128";
                }
                //获取路径
                text = path.getText();
                File file = new File(text);
                if (!file.exists()) {
                    file.mkdir();
                    area.append("                                                           创建文件夹成功！\n");
                }

                //获取关键字
                stopText = antiStop.getText();
                //获取图片大小
                sizeS = (String) size.getSelectedItem();
                if (sizeS.equals("全部尺寸")) {
                    sizeS = "0";
                }
                if (sizeS.equals("特大尺寸")) {
                    sizeS = "9";
                }
                if (sizeS.equals("大尺寸")) {
                    sizeS = "3";
                }
                if (sizeS.equals("中尺寸")) {
                    sizeS = "2";
                }
                if (sizeS.equals("小尺寸")) {
                    sizeS = "1";
                }

                threadDowLoad = new Thread(new Runnable() {
                    @Override
                    public void run() {
                            DowLoadUtil.DowloadGUI(stopText, encode, outputStream, inputStream, text, sizeS, colors, name, c, (pages * 30), area);
                    }
                });
                threadDowLoad.start();

            }
        });


    }


    public void set() {
        dialogSet = new JDialog(this, "设置面板", true);
        dialogSet.setBounds(500, 80, 274, 700);
        dialogSet.setLayout(new FlowLayout());
        //设置颜色标签
        JLabel labelColor = new JLabel("设置颜色");

        //设置颜色下拉框
        final JComboBox<String> setFColorList = new JComboBox<>(setColorList);


        //设置颜色应用按钮
        JButton setColor = new JButton("应用颜色");
        setColor.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String colorSelect = (String) setFColorList.getSelectedItem();
                if (colorSelect.equals("无颜色")) {
                    contentPane.setBackground(null);
                }
                if (colorSelect.equals("黑色")) {
                    contentPane.setBackground(Color.DARK_GRAY);
                }
                if (colorSelect.equals("红色")) {
                    contentPane.setBackground(Color.red);
                }
                if (colorSelect.equals("绿色")) {
                    contentPane.setBackground(Color.GREEN);
                }
                if (colorSelect.equals("蓝色")) {
                    contentPane.setBackground(Color.BLUE);
                }
                if (colorSelect.equals("白色")) {
                    contentPane.setBackground(Color.white);
                }
            }
        });

        //设置颜色标签
        JLabel labelFont = new JLabel("设置字体");
        //设置颜色下拉框
        final JComboBox<String> setFFontList = new JComboBox<>(setFontList);
        //设置字体按钮
        JButton setFont = new JButton("应用字体");
        setFont.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectedItem = (String) setFFontList.getSelectedItem();
                if (selectedItem.equals("微软雅黑")) {
                    contentPane.setFont(font2);
                    jLabel2.setFont(font2);
                    path.setFont(font2);
                    jLabel.setFont(font2);
                    antiStop.setFont(font2);
                    jLabelSize.setFont(font2);
                    jLabel3.setFont(font2);
                    page.setFont(font2);
                    jLabel1.setFont(font2);
                    contentPane.validate();
                }

                if (selectedItem.equals("宋体")) {
                    contentPane.setFont(font1);
                    jLabel2.setFont(font1);
                    path.setFont(font1);
                    jLabel.setFont(font1);
                    antiStop.setFont(font1);
                    jLabelSize.setFont(font1);
                    jLabel3.setFont(font1);
                    page.setFont(font1);
                    jLabel1.setFont(font1);
                    contentPane.validate();
                }
                if (selectedItem.equals("楷体")) {
                    contentPane.setFont(font3);
                    jLabel3.setFont(font3);
                    path.setFont(font3);
                    jLabel.setFont(font3);
                    antiStop.setFont(font3);
                    jLabelSize.setFont(font3);
                    jLabel3.setFont(font3);
                    page.setFont(font3);
                    jLabel1.setFont(font3);
                    contentPane.validate();
                }
            }
        });

        ClassLoader cl = this.getClass().getClassLoader();
        URL url = cl.getResource("a.jpg");
        String decode = null;
        try {
            decode = URLDecoder.decode(url.getPath(),"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
/*        String path = SwingMain.class.getResource("/").getPath();
        String s = URLDecoder.decode(path + "a.jpg");
        System.out.println(s);*/
        ImageIcon imageIcon = new ImageIcon(decode);
        imageIcon.setImage(imageIcon.getImage().getScaledInstance(150, 150, Image.SCALE_DEFAULT));
        JLabel img = new JLabel(imageIcon);

        dialogSet.add(labelColor);
        dialogSet.add(setFColorList);
        dialogSet.add(setColor);
        dialogSet.add(labelFont);
        dialogSet.add(setFFontList);
        dialogSet.add(setFont);
        dialogSet.add(new JLabel("作者:hong-xiaohan"));
        dialogSet.add(img);

    }

    public static void main(String[] args) {
        new SwingMain();

    }

}
